package com.vpx.server.mapper.system;

import com.vpx.server.framework.base.BaseMapper;
import com.vpx.server.bean.entity.SysMenu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module 菜单
 * @description: 菜单Mapper
 * @date 2021/9/7 19:15
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 查询子菜单
     * @param parentId
     * @return
     */
    @SelectProvider(type = SysMenuMapper.SqlProvider.class, method = "queryMenuByParentId")
    List<SysMenu> querySysMenuByParentId(@Param("parentId") Long parentId);

    /**
     * 查询子菜单数量
     * @param parentId
     * @return
     */
    @SelectProvider(type = SysMenuMapper.SqlProvider.class, method = "queryMenuChildCount")
    int querySysMenuChildCount(@Param("parentId") Long parentId);

    /**
     * 根据编码查询菜单记录条数
     * @param code
     * @return
     */
    @SelectProvider(type = SysMenuMapper.SqlProvider.class, method = "queryMenuCountByCode")
    int querySysMenuCountByCode(@Param("code")String code);

    /**
     * 查询所有的非父级菜单
     * @return
     */
    @Select("select * from sys_menu where menu_type = 'MENU'")
    List<SysMenu> queryAllSysMenu();

    /**
     * 根据id和编码查询菜单记录条数
     * @param id
     * @param code
     * @return
     */
    @Select("select count(0) from sys_menu where id = #{id} and code = #{code}")
    int querySysMenuCountByIdAndCode(@Param("id") Long id,@Param("code")String code);

    class SqlProvider {
        public String queryMenuByParentId(@Param("parentId") Long parentId){
            String sql = "select * from sys_menu where parent_id = #{parentId} order by sort_num";
            return sql;
        }

        public String queryMenuChildCount(@Param("parentId") Long parentId){
            String sql = "select count(0) from sys_menu where parent_id = #{parentId}";
            return sql;
        }

        public String queryMenuCountByCode(@Param("code")String code){
            String sql = "select count(0) from sys_menu where code = #{code}";
            return sql;
        }

    }
}
